#!/bin/bash
#
# @(#) This should build the toolchain on Leopard 10.5.
# @(#) 2009, George Hrysanthopoulos, george@softekpartners.com
# @(#) This file is released under the GNU License
#
WHOAMI=$USER
HOMEDIR=`pwd`
export HEAVENLY="${HOMEDIR}/iphone-filesystem"
export LLVMOBJDIR="${HOMEDIR}/llvm-svn"
unset LD_LIBRARY_PATH						# This has come back to bite me
CLEAN=0
STAGE=0
#
# Stage 0. Initial checks.
	while [[ "$1" == -* ]]
	do
		if [[ "$1" == "-c" || "$1" == "-clean" ]]
		then
			CLEAN=1
			echo -e "\nNote: The software will be reloaded from the net.\n"
			shift
		elif [[ "$1" == "-s" ]]
		then
			shift
			STAGE=$1
			if (( STAGE > 8 ))
			then
				echo -e "Stage must be 1-7"
				exit 1
			else
				let STAGE=STAGE+0
				echo -e "\nStarting at stage: ${STAGE}"
			fi
			shift
		else
			echo -e "\nUsage: $0 [-c]\n\n\tWhere:\n\t\t-c Remove any old build files, re-download from internet and rebuild everything."
			echo -e "\t\t-s nstage Start from stage: nstage"
			exit
		fi
	done
	#
	# Check that we are Darwin 10.5 and above
		SWVERS=$(sw_vers | grep 'ProductVersion:' | grep -o '[0-9]*\.[0-9]*\.[0-9]*')
		if [[ ${SWVERS} != "10.5"* ]]
		then
			echo "Note: System software version must be >= 10.5 Currently we get: ${SWVERS}"
			exit
		fi
	#
	# Tell the user what he needs to prep
	echo -e "\nBefore you run this script, make sure you have:"
		echo -e "\n\t1. Jailbroken your iPhone."
		echo -e "\n\t2. Xcode development system loaded"
		echo -e "\n\t3. From the iPhone:\n\t\tiphone-root# apt-get install ldid\n\t   This will install ldid which is needed to sign the iPhone applications."
		echo -e "\n\t4. Downloaded the iPhone filesystem to directory:\n\t\tiphone-filesystem"
		if [[ -d "iphone-filesystem" ]]
		then
			echo -e "\t\tNote: Directory currently exists\n"
		else
			echo -e "\t\tNote: Directory currently does not exist\n"
		fi
		#
		echo -e "\t   Example:\n\t\tmkdir iphone-filesystem; ssh root@iphone_IP_address \"(cd / && tar -cf - *)\" | (cd iphone-filesystem && tar -xf -)\n"
		echo -e "\t5. Downloaded \"lib1funcs.asm\" patch for Leopard > V10.5"
		#
		if [[ ! -f lib1funcs.asm.diff ]]
		then
			echo -e "\n\t   Note: Patch file: \"lib1funcs.asm.diff\" does not exist in the current directory. You must download from the internet."
			echo -e "\t   You can find it at: \"http://iphone-dev.googlecode.com/issues/attachment?aid=-4811806783580725987&name=lib1funcs.asm.diff\""
		else
			echo -e "\n\t   Note: Patch file: \"lib1funcs.asm.diff\" exists in the current directory."
		fi
		echo ""; read -p "All above steps are done? [y/N]" line
		if [[ "${line}" = "n" || "${line}" = "N" || "${line}" = "" ]]
		then
			exit
		fi
	#
	echo -e "\nSoftware tools versions:\n"
		if ! which bison >/dev/null 2>&1
		then
			echo -e "Utility: \"bison\" does not exist. Please install xcode and re-start"
			exit 1
		fi
		if ! which flex >/dev/null 2>&1
		then
			echo -e "Utility: \"flex\" does not exist. Please install xcode and re-start"
			exit 1
		fi
		echo "bison"
		bison --version 2>&1 | sed 's/^/	/'
		echo "flex"
		flex --version 2>&1 | sed 's/^/	/'
	echo -e "\nBuilding tools...\n"
#
# Stage 1
	if (( STAGE <= 1 ))
	then
		if (( CLEAN ))
		then
			rm -rf llvm-svn
			svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm-svn -r 42498 || exit 1
		fi
		pushd llvm-svn >/dev/null || exit 1
			./configure --enable-optimized
			make ENABLE_OPTIMIZED=1 || exit 1
			echo -e "\nAbout to \"sudo\" in order to install llvm-svn.\n"
			sudo make install
			echo ""; read -p "Stage 1 complete. Hit Enter to continue" line
		popd >/dev/null
	fi
#
# Stage 2
	if (( STAGE <= 2 ))
	then
		if (( CLEAN ))
		then
			rm -rf iphone-dev
			svn checkout http://iphone-dev.googlecode.com/svn/trunk/ iphone-dev || exit 1
			#
			pushd iphone-dev >/dev/null || exit 1
				pushd include >/dev/null || exit 1
					svn switch http://iphone-dev.googlecode.com/svn/branches/include-1.2-sdk
				popd >/dev/null
				pushd odcctools >/dev/null || exit 1
					svn switch http://iphone-dev.googlecode.com/svn/branches/odcctools-9.2-ld
				popd >/dev/null
			popd >/dev/null
			echo -e "\nAbout to \"sudo\" in order to \"rm -f, mkdir -p /usr/local/arm-apple-darwin\"\n"
			sudo rm -rf /usr/local/arm-apple-darwin
			sudo mkdir -p /usr/local/arm-apple-darwin
		fi
		pushd iphone-dev >/dev/null || exit 1
			mkdir -p build/odcctools
			pushd build/odcctools >/dev/null || exit 1
				#
				# The following line fixes a bug
				../../odcctools/configure --target=arm-apple-darwin --disable-ld64
				export INCPRIVEXT="-isysroot /Developer/SDKs/MacOSX10.4u.sdk"
				make || exit 1
				echo -e "\nAbout to \"sudo\" in order to \"make install\"\n"
				sudo make install
			popd >/dev/null		# iphone-dev
			echo ""; read -p "Stage 2 complete. Hit Enter to continue" line
		popd >/dev/null
	fi
#
# Stage 3
# Process patch
	if (( STAGE <= 3 ))
	then
		if [[ ! -f ./iphone-dev/llvm-gcc-4.0-iphone/gcc/config/arm/lib1funcs.asm ]]
		then
			echo -e "\"./iphone-dev/llvm-gcc-4.0-iphone/gcc/config/arm/lib1funcs.asm\" does not exist. Try running:"
			echo -e "\t$0 -c"
			exit 1
		else
			TPAT=$(head -670 ./iphone-dev/llvm-gcc-4.0-iphone/gcc/config/arm/lib1funcs.asm | tail -1)
			if [[ "${TPAT}" = *11f* ]]
			then
				echo -e "Patch has not been applied to \"lib1funcs.asm\"\n"
				read -p "You would you like to apply it now? [y/N]" line
				if [[ "${line}" = "n" || "${line}" = "N" || "${line}" = "" ]]
				then
					echo "You must apply patch or build will fail.\n"
					echo -e "\tTo apply patch for Leopard > V10.5 do:\n\t\tpatch -i lib1funcs.asm.diff ./iphone-dev/llvm-gcc-4.0-iphone/gcc/config/arm/lib1funcs.asm"
					echo -e "\tTo test the patch try this:\n\t\tpatch -i lib1funcs.asm.diff --dry-run ./iphone-dev/llvm-gcc-4.0-iphone/gcc/config/arm/lib1funcs.asm"
					exit
				fi
				echo -e "\tApplying patch for Leopard :\n\t\tpatch -i lib1funcs.asm.diff ./iphone-dev/llvm-gcc-4.0-iphone/gcc/config/arm/lib1funcs.asm\n"
				patch -i lib1funcs.asm.diff ./iphone-dev/llvm-gcc-4.0-iphone/gcc/config/arm/lib1funcs.asm
				RC=$?
				if (( ! RC ))
				then
					echo ""; read -p "Patch complete. Hit Enter to continue" line
				else
					echo ""; echo -n "Patch application failed.\n";exit 1
				fi
			else
				echo -e "Patch has been applied to \"lib1funcs.asm\". We continue\n"
			fi
		fi
		echo ""; read -p "Stage 3 complete. Hit Enter to continue" line
	fi
#
# Stage 4
	if (( STAGE <= 4 ))
	then
		echo -e "HEAVENLY (iPhone filesystem directory) is set to: \"${HEAVENLY}\""
		if [[ ! -d "${HEAVENLY}" ]]
		then
			echo -e "Note: \"HEAVENLY\" directory, does not exist. Please correct this before continuing and re-run from stage 4"
			exit 1
		fi
		echo ""; read -p "Stage 4 complete. Hit Enter to continue" line
	fi
#
# Stage 5
	if (( STAGE <= 5 ))
	then
		pushd iphone-dev >/dev/null || exit 1
			pushd include >/dev/null || exit 1
				./configure --with-macosx-sdk=/Developer/SDKs/MacOSX10.4u.sdk
				echo -e "\nAbout to \"sudo\" in order to \"install-headers.sh\"\n"
				sudo bash install-headers.sh
			popd >/dev/null		# iphone-dev
			echo ""; read -p "Stage 5 complete. Hit Enter to continue" line
		popd >/dev/null
	fi
#
# Stage 6
	if (( STAGE <= 6 ))
	then
		pushd iphone-dev >/dev/null || exit 1
			mkdir -p build/csu
			pushd build/csu >/dev/null || exit 1
				../../csu/configure --host=arm-apple-darwin
				echo -e "\nAbout to \"sudo\" in order to \"make install\"\n"
				sudo make install
			popd >/dev/null		# iphone-dev
			echo ""; read -p "Stage 6 complete. Hit Enter to continue" line
		popd >/dev/null
	fi
#
#
# Stage 7 and stage 8 must be run together
	if (( STAGE <= 7 ))
	then
		pushd iphone-dev >/dev/null || exit 1
			if [[ -f llvm-gcc-4.0-iphone/configure ]]
			then
				if [[ -f llvm-gcc-4.0-iphone/configure.old ]]
				then
					echo -e "Both versions of configure are active. We will ovewrite the active from the old"
					ls -ld llvm-gcc-4.0-iphone/configure llvm-gcc-4.0-iphone/configure.old
					cp -p llvm-gcc-4.0-iphone/configure.old llvm-gcc-4.0-iphone/configure
				else
					echo -e "Only the new version of configure exists"
					ls -ld llvm-gcc-4.0-iphone/configure
					mv llvm-gcc-4.0-iphone/configure llvm-gcc-4.0-iphone/configure.old
				fi
				sed 's/^FLAGS_FOR_TARGET=$/FLAGS_FOR_TARGET=${FLAGS_FOR_TARGET-}/g' llvm-gcc-4.0-iphone/configure.old > llvm-gcc-4.0-iphone/configure
				echo ""
				#
				export FLAGS_FOR_TARGET="-mmacosx-version-min=10.1"
				echo -e "Enter root password:"
				echo -e "\nAbout to \"sudo\" in order to create \"/usr/local/arm-apple-darwin/lib/crt1.10.5.o\"\n"
				sudo bash -c "(rm -f /usr/local/arm-apple-darwin/lib/crt1.10.5.o; ln -s /usr/local/arm-apple-darwin/lib/crt1.o /usr/local/arm-apple-darwin/lib/crt1.10.5.o;)"
				echo ""; read -p "Stage 7 complete. Hit Enter to continue" line
			#
			# Stage 8
				#
				[[ ! -d build/llvm-gcc-4.0-iphone ]] && mkdir -p build/llvm-gcc-4.0-iphone
				pushd build/llvm-gcc-4.0-iphone >/dev/null || exit 1
					#
					echo -e "\nCheck \"../../llvm-gcc-4.0-iphone/configure\""
					ls -ld ../../llvm-gcc-4.0-iphone/configure
					chmod 755 ../../llvm-gcc-4.0-iphone/configure
					#
					../../llvm-gcc-4.0-iphone/configure \
						--enable-llvm=`llvm-config --obj-root` --enable-languages=c,c++,objc,obj-c++ \
						--target=arm-apple-darwin --enable-sjlj-exceptions --with-heavenly="${HEAVENLY}" \
						--with-as=/usr/local/bin/arm-apple-darwin-as --with-ld=/usr/local/bin/arm-apple-darwin-ld \
						--enable-wchar_t=no
					sleep 10
					#
					make clean
					make LLVM_VERSION_INFO=2.0-svn-iphone-dev-0.3-svn
					RC=$?
					if (( ! RC ))
					then
						echo -e "\nSuccess"'!'
					fi
				popd >/dev/null
			else
				echo -e "\"$(pwd)/llvm-gcc-4.0-iphone/configure\" does not exit. Bye"
				exit 1
			fi
		popd >/dev/null
	fi
